<?php

/**
 * ECSHOP 模板类
 */

class template
{
    /**
    * 用来存储变量的空间
    *
    * @access  private
    * @var     array      $vars
    */
    var $vars = array();

   /**
    * 模板存放的目录路径
    *
    * @access  private
    * @var     string      $path
    */
    var $path = '';

    /**
     * 构造函数
     *
     * @access  public
     * @param   string       $path
     * @return  void
     */
    function __construct($path)
    {
        $this->template($path);
    }

    /**
     * 构造函数
     *
     * @access  public
     * @param   string       $path
     * @return  void
     */
    function template($path)
    {
        $this->path = $path;
    }

    /**
     * 模拟smarty的assign函数
     *
     * @access  public
     * @param   string       $name    变量的名字
     * @param   mix           $value   变量的值
     * @return  void
     */
    function assign($name, $value)
    {
        $this->vars[$name] = $value;
    }

    /**
     * 模拟smarty的fetch函数
     *
     * @access  public
     * @param   string       $file   文件相对路径
     * @return  string      模板的内容(文本格式)
     */
    function fetch($file)
    {
        extract($this->vars);
        ob_start();
        include($this->path . $file);
        $contents = ob_get_contents();
        ob_end_clean();

        return $contents;
    }

    /**
     * 模拟smarty的display函数
     *
     * @access  public
     * @param   string       $file   文件相对路径
     * @return  void
     */
    function display($file)
    {
		
		$str = $this->fetch($file);
		
		
		$str = str_replace("__ORG__",MODULE."/views/__ORG__",$str);
		
		// 来自：ecshop
		//$str = preg_replace('/((?:background|src)\s*=\s*["|\'])(?:\.\/|\.\.\/)?(images\/.*?["|\'])/is',$str); // 在images前加上 $tmp_dir
		//$str = preg_replace('/((?:background|background-image):\s*?url\()(?:\.\/|\.\.\/)?(images\/)/is',$str);// 在images前加上 $tmp_dir
		
		
		
		$str = preg_replace("/([\n\r]+)\t+/s","\\1",$str);
		$str = preg_replace("/\<\!\-\-\{(.+?)\}\-\-\>/s", "{\\1}",$str);
		$str = preg_replace("/\{template\s+(.+)\}/","<?php include template(\\1); ?>",$str);
		$str = preg_replace("/\{include\s+(.+)\}/","<?php include \\1; ?>",$str);
		$str = preg_replace("/\{php\s+(.+)\}/","<?php \\1?>",$str);
		$str = preg_replace("/\{if\s+(.+?)\}/","<?php if(\\1) { ?>",$str);
		$str = preg_replace("/\{else\}/","<?php } else { ?>",$str);
		$str = preg_replace("/\{elseif\s+(.+?)\}/","<?php } elseif (\\1) { ?>",$str);
		$str = preg_replace("/\{\/if\}/","<?php } ?>",$str);
		$str = preg_replace("/\{loop\s+(\S+)\s+(\S+)\}/","<?php if(is_array(\\1)) foreach(\\1 AS \\2) { ?>",$str);
		$str = preg_replace("/\{loop\s+(\S+)\s+(\S+)\s+(\S+)\}/","<?php if(is_array(\\1)) foreach(\\1 AS \\2 => \\3) { ?>",$str);
		$str = preg_replace("/\{\/loop\}/","<?php } ?>",$str);
		$str = preg_replace("/\{\/get\}/","<?php } unset(\$DATA); ?>",$str);
		$str = preg_replace("/\{tag_([^}]+)\}/e", "get_tag('\\1')", $str);
		$str = preg_replace("/\{get\s+([^}]+)\}/e", "get_parse('\\1')", $str);
		$str = preg_replace("/\{([a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff:]*\(([^{}]*)\))\}/","<?php echo \\1;?>",$str);
		$str = preg_replace("/\{\\$([a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff:]*\(([^{}]*)\))\}/","<?php echo \\1;?>",$str);
		$str = preg_replace("/\{(\\$[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*)\}/","<?php echo \\1;?>",$str);
		$str = preg_replace("/\{([A-Z_\x7f-\xff][A-Z0-9_\x7f-\xff]*)\}/s", "<?php echo \\1;?>",$str);
        //echo eval($str);
		
		//return $str;
		echo $str;
    }
	
	/* 静态化 */
	function html($file_name, $content) 
     {     //目录不存在就创建 
         if (!file_exists (dirname($file_name))) { 
              if (!@mkdir (dirname($file_name), 0777)) { 
                      die($file_name."目录创建失败！"); 
              } 
          } 
                     
          if(!$fp = fopen($file_name, "w")){ 
              echo "文件打开失败！"; 
              return false; 
          } 


          if(!fwrite($fp, $content)){ 
              echo "文件写入失败！"; 
             fclose($fp); 
              return false; 
          } 
         
         fclose($fp);
             chmod($file_name,0666);
      } //这个函数的作用就是保存文件～～
}
/*这个URL在新闻入库的时候就要生成, 具体的过程是这样的: 
1. 页面填写新闻内容, 提交表单 
2. 按时间或是你自定义的格式生成URL 
3. 拼接SQL语句, 将新闻内容写入数据库, 注意这时也要将URL的值写入 
4. 取得模板页面内容, 按URL地址生成静态页 */


?>